<template>
  <div>
    <Button icon="md-download" :loading="exportLoading" @click="exportExcel">导出文件</Button>
  </div>
</template>
<script>
import excel from '@/libs/excel'
export default {
  name: 'btn-export',
  props: {
    tableData: null,
    tableTitle: null,
    title: ''
  },
  data () {
    return {
      exportLoading: false,
      newTableTitle: [],
      newTableData: [],
      newTableKey: []
    }
  },
  methods: {
    exportExcel () {
      if (this.tableData.length) {
        this.exportLoading = true
        const params = {
          title: this.newTableTitle.slice(1, -1),
          key: this.newTableKey.slice(1, -1),
          data: this.tableData,
          autoWidth: true,
          filename: this.title
        }
        excel.export_array_to_excel(params)
        this.exportLoading = false
      } else {
        this.$Message.info('表格数据不能为空！')
      }
    },
    getNewTableTitle () {
      this.tableTitle.map((item) => {
        this.newTableTitle.push(item.title)
      })
    },
    getNewTableKey () {
      this.tableTitle.map((item) => {
        this.newTableKey.push(item.key)
      })
    }
  },
  created () {
    this.getNewTableTitle()
    this.getNewTableKey()
  },
  mounted () {

  }
}
</script>
